package handling.login.handler;

import client.MapleClient;
import client.MapleEnumClass;
import handling.login.LoginServer;
import handling.login.LoginWorker;
import java.util.Calendar;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import tools.DateUtil;
import tools.MaplePacketCreator;
import tools.Pair;
import tools.StringUtil;
import tools.data.input.LittleEndianAccessor;
import tools.packet.LoginPacket;

public class LoginPasswordHandler {
    private static final Logger log = LogManager.getLogger(MapleClient.class);

    public static void handlePacket(LittleEndianAccessor slea, MapleClient c) throws Exception {

        slea.skip(22);
        String login = slea.readMapleAsciiString();
        String pwd = slea.readMapleAsciiString();
        boolean isIpBan = c.hasBannedIP();
        boolean isMacBan = c.hasBannedMac();
        boolean isBanned = (isIpBan || isMacBan);
        c.setTempInfo(login, pwd, isBanned);
        if (isBanned) {
            c.clearInformation();
            c.announce(MaplePacketCreator.serverNotice(1, "您的账号已被封停！"));
            c.announce(LoginPacket.getLoginFailed(MapleEnumClass.AuthReply.GAME_DEFINITION_INFO));
            return;
        }
        Login(c);
    }

    public static void Login(MapleClient c) {
        System.out.println(c.getTempInfo().toString());
        String login = (String)c.getTempInfo().getLeft();
        String pwd = (String)c.getTempInfo().getMid();
        Boolean isBanned = (Boolean)c.getTempInfo().getRight();
        MapleEnumClass.AuthReply loginok = c.login(login, pwd, isBanned.booleanValue(), false);
        Calendar tempbannedTill = c.getTempBanCalendar();
        if (tempbannedTill != null &&
                tempbannedTill.getTimeInMillis() > System.currentTimeMillis()) {
            c.clearInformation();
            long tempban = DateUtil.getTempBanTimestamp(tempbannedTill.getTimeInMillis());
            c.announce(LoginPacket.getTempBan(tempban, c.getBanReason()));
            return;
        }
        if (loginok.is(MapleEnumClass.AuthReply.GAME_ACCOUNT_DELETE) && !isBanned.booleanValue()) {
            c.clearInformation();
            c.announce(LoginPacket.getTempBan(2147483647L, c.getBanReason()));
        } else if (loginok.is(MapleEnumClass.AuthReply.GAME_LOGIN_SUCCESSFUL) && isBanned.booleanValue() && !c.isGm()) {
            c.announce(LoginPacket.getPermBan((byte)1));
        } else if (!loginok.is(MapleEnumClass.AuthReply.GAME_LOGIN_SUCCESSFUL)) {
            c.clearInformation();
            c.announce(LoginPacket.getLoginFailed(loginok));
        } else if (c.getGender() == 10) {
            c.updateLoginState(4);
            c.announce(LoginPacket.genderNeeded(c));
        } else {
            c.loginAttempt = 0;
            c.updateMacs();
            LoginWorker.registerClient(c, false);
        }
    }

    public static void handlerAuthKey(LittleEndianAccessor slea, MapleClient c) {
        String key = slea.readMapleAsciiString();
        Pair<String, Integer> clientinfo = LoginServer.getLoginAuthKey(key, false);
        c.login((String)clientinfo.getLeft(), "", false, true);
        c.loginAttempt = 0;
        LoginWorker.registerClient(c, true);
    }

    public static void handlerAuth(LittleEndianAccessor slea, MapleClient c) {
        ServerlistRequestHandler.handlePacket(c, false);
    }
}
